package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"ALL"})
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 套餐的分页查询
     *
     * @return
     */
    @Override
    public PageResult Page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealVOS = setmealMapper.pageSelect(setmealPageQueryDTO);
        Page<Setmeal> page = (Page) setmealVOS;
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Integer id) {
        //查询套餐除了菜品
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询套餐中的菜品
        List<SetmealDish> setmealDish = setmealDishMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        List<SetmealDish> setmealDish;
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealDish = setmealDTO.getSetmealDishes();
        setmealDish.stream()
                .forEach(setmealDish1 -> setmealDish1.setSetmealId(setmeal.getId()));
        //修改套餐表除了菜品
        setmealMapper.update(setmeal);
        //修改套餐中的菜品
        setmealDishMapper.deleteById(setmeal.getId());
        setmealDishMapper.insert(setmealDish);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */

    @Transactional
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        List<SetmealDish> setmealDish;
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //新增套餐表除了菜品
        setmealMapper.insert(setmeal);
        setmealDish = setmealDTO.getSetmealDishes();
        if (setmealDish != null && setmealDish.size() != 0) {
            setmealDish.stream()
                    .forEach(setmealDish1 -> setmealDish1.setSetmealId(setmeal.getId()));
            //新增套餐中的菜品
            log.info("输出{}", setmealDish);
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        //删除套餐表，和套餐菜品表

        int nums =  setmealMapper.selectStatus(ids);
        if (nums != 0){
            return StatusConstant.ON_SALE;
        }
        for (Long id : ids) {
            setmealMapper.deleteByid(id);
            setmealDishMapper.deleteById(id);
        }
        return 0;
    }

    /**
     * 是否售卖
     * @param status
     * @param id
     */
    @Override
    public void isSale(Integer status, Long id) {
        setmealMapper.updateSale(status,id);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
